// pagesMine/pages/landlordManage/landlordManage.js
import ajax from '../../../utils/index'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    /**
     * tab 状态 1全部房产 2正在出租
     */
    tabActive: 1,
    /**
     * 房东认证状态 0未认证；1已认证；-1已屏蔽
     */
    authenticationArr: ['已屏蔽', '未认证', '已认证'],
    /**
     * 房东类型：1园区招商部 2开发商、一房东 3、二房东 4
     */
    typeArr: ['园区招商部', '开发商', '一房东', '二房东'],
    /**
     * 用户信息
     */
    userInfo: null,
    /**
     * 详情对象  房东或经纪人
     */
    detailEntity: null,
    /**
     * 列表查询对象
     */
    formData: {
      total: 0,
      pageSize: 10,
      pageNum: 1,
      type: 0,// 用户身份 0房东 1经纪人
    },
    /**
     * 房源/出租数量 对象
     */
    countEntity: {
      leaseHousing: 0, // 出租数量 
      wholeHousing: 0 // 房源数量 
    },
    /**
     * 正在出租列表数据
     */
    dataList: [],
    /**
     * 房源列表数据
     */
    housePropertyList: [],

  },
  /**
   * 头像上传事件
   */
  async headPortraitUpload() {
    const detailEntity = this.data.detailEntity

    const filePath = await ajax.chooseImage()
    const res = await ajax.uploadFile({ filePath })
    const formData = { avatar: res.data.url, id: detailEntity.id }
    this.data.formData.type == 1 ? await ajax.post('/my/house/agent/update', formData) : await ajax.post('/my/house/master/update', formData)
    ajax.showSuccess('操作成功')
    this.data.formData.type == 1 ? this.getAgentInfo() : this.getHouseInfo()
  },
  /**
   * 房源上架 / 下架 事件
   */
  async putawayEvent(event) {
    const { index, item } = event.currentTarget.dataset;
    const { dataList } = this.data
    if (item.rentStatus == 1) return ajax.showToast('房源已出租');
    ajax.showLoading();
    const shelfStatus = item.shelfStatus == 1 ? 0 : 1;
    const rentStatus = shelfStatus == 1 ? 2 : 0;
    await ajax.post('/my/house/rent/upAndDownRent', { id: item.id, shelfStatus, rentStatus }).finally(() => wx.hideLoading());
    ajax.showSuccess('操作成功');
    item.rentStatus = rentStatus
    item.shelfStatus = shelfStatus
    dataList[index] = item
    this.setData({ dataList });
  },
  /**
   * 删除房源
   */
  houseDelete(event) {
    const { item, index } = event.currentTarget.dataset
    if (item.shelfStatus == 1) return ajax.showToast('请下架后进行操作')
    const { dataList } = this.data
    wx.showModal({
      title: "提示",
      content: '确认删除房源信息吗',
      success: async (res) => {
        if (!res.confirm) return
        await ajax.get(`/my/house/rent/remove?id=${item.id}`);
        await ajax.showSuccess('操作成功!')
        dataList.splice(index, 1)
        this.setData({ dataList })
        this.getCountRequest()
      }
    })
  },
  /**
   * 删除房产
   */
  housingDelete(event) {
    const { item, index } = event.currentTarget.dataset
    const { housePropertyList } = this.data
    wx.showModal({
      title: "提示",
      content: '确认删除房产信息吗?',
      success: async (res) => {
        if (!res.confirm) return
        await ajax.get(`/my/house/assess/remove?id=${item.id}`);
        await ajax.showSuccess('操作成功!')
        housePropertyList.splice(index, 1)
        this.setData({ housePropertyList })
        this.getCountRequest()
      }
    })
  },
  /**
   * tab点击事件
   */
  tabEvent(event) {
    let { formData, tabActive } = this.data
    const value = event.currentTarget.dataset.value
    if (tabActive == value) return
    tabActive = value
    formData.pageNum = 1
    this.setData({ formData, tabActive })
    tabActive == 1 ? this.getHousePropertyList() : this.getHousinglist()
  },
  /**
   * 获取全部房源和正在出租数量
   */
  async getCountRequest() {
    const res = await ajax.get('/my/house/master/housingCount', this.data.formData)
    this.setData({ countEntity: res.data })
  },
  /**
   * 正在出租(列表)
   */
  async getHousinglist() {
    const { formData, dataList } = this.data
    const res = await ajax.get('/my/house/master/housinglist', formData).finally(() => wx.stopPullDownRefresh())
    const list = res.rows
    list.forEach(item => item.roomNumber = item?.roomNumber?.replace(/,/g, '-') || '')
    formData.total = res.total
    this.setData({ formData, dataList: formData.pageNum == 1 ? list : dataList.concat(list) })
  },
  /**
   * 房产列表
   */
  async getHousePropertyList() {
    const { formData, housePropertyList } = this.data
    const res = await ajax.get('/my/house/master/housePropertyList', formData).finally(() => wx.stopPullDownRefresh())
    const list = res.rows
    formData.total = res.total
    this.setData({ formData, housePropertyList: formData.pageNum == 1 ? list : housePropertyList.concat(list) })
  },
  /**
   * 房东详情
   */
  async getHouseInfo() {
    const { userInfo } = this.data
    const homeownerId = userInfo.roles.homeowner_id
    const res = await ajax.get(`/my/house/master/info?id=${homeownerId}`)
    this.setData({ detailEntity: res.data })
  },
  /**
   * 经纪人详情
   */
  async getAgentInfo() {
    const { userInfo } = this.data
    const agentId = userInfo.roles.agent_id
    const res = await ajax.get(`/my/house/agent/info?id=${agentId}`)
    this.setData({ detailEntity: res.data })
  },
  /**
   * 获取用户详情
   */
  async getUserInfo() {
    const userInfo = await ajax.getUserInfo()
    this.setData({ userInfo: userInfo })
  },
  /**
   * 初始化
   */
  async infoRequest() {
    this.data.formData.pageNum = 1
    await this.getUserInfo()
    this.data.formData.type == 0 ? this.getHouseInfo() : this.getAgentInfo()
    this.getCountRequest()
    this.data.tabActive == 1 ? this.getHousePropertyList() : this.getHousinglist()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({ title: !options.type ? '招商管理' : '经纪人主页' })
    this.setData({ ['formData.type']: options.type || 0 })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.infoRequest()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.infoRequest()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    const { tabActive, dataList, housePropertyList, formData } = this.data
    const total = formData.total
    if (tabActive == 1 && total == housePropertyList.length) return;
    if (tabActive == 2 && total == dataList.length) return;
    this.data.formData.pageNum += 1;
    tabActive == 1 ? this.getHousePropertyList() : this.getHousinglist()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})